package com.xiaomi.method;

import com.xiaomi.pojo.Employee;
import com.xiaomi.pojo.EmployeeMessage;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class EmployeeSystem {

    /**
     * 新增员工信息
     * @param list
     */
    public static void add(List<Employee> list) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println(EmployeeMessage.INPUT_ID);
            int id = scanner.nextInt();
            // 检查ID是否已存在
            boolean idExists = false;
            for (Employee emp : list) {
                if (emp.getId() == id) {
                    System.out.println(EmployeeMessage.EXIST_EMPLOYEE);
                    idExists = true;
                    break;
                }
            }
            if (idExists) {
                continue;
            }
            System.out.println(EmployeeMessage.INPUT_NAME);
            String name = scanner.next();
            System.out.println(EmployeeMessage.INPUT_AGE);
            int age = scanner.nextInt();
            String phone;
            while (true) {
                System.out.println(EmployeeMessage.INPUT_PHONE);
                phone = scanner.next();
                if (phone.length() == 11) {
                    break;
                } else {
                    System.out.println(EmployeeMessage.INPUT_MISTAKE);
                }
            }
            String idCard;
            while (true) {
                System.out.println(EmployeeMessage.INPUT_IDCARD);
                idCard = scanner.next();
                if (idCard.length() == 18) {
                    break;
                } else {
                    System.out.println(EmployeeMessage.INPUT_MISTAKE);
                }
            }
            System.out.println(EmployeeMessage.INPUT_ADDRESS);
            String address = scanner.next();
            System.out.println(EmployeeMessage.INPUT_SEX);
            String gender = scanner.next();
            // 检查性别输入是否正确
            while (!gender.equals(EmployeeMessage.MALE_EMPLOYEE) && !gender.equals(EmployeeMessage.FEMALE_EMPLOYEE)) {
                System.out.println(EmployeeMessage.INPUT_MISTAKE);
                gender = scanner.next();
            }

            System.out.println(EmployeeMessage.INPUT_POSITION);
            String position = scanner.next();
            System.out.println(EmployeeMessage.INPUT_SALARY);
            double salary = scanner.nextDouble();

            Employee employee = new Employee();
            employee.setId(id);
            employee.setName(name);
            employee.setAge(age);
            employee.setPhone(phone);
            employee.setIdCard(idCard);
            employee.setAddress(address);
            employee.setGender(gender);
            employee.setJob(position);
            employee.setSalary(salary);
            employee.setHireDate(LocalDateTime.now());
            employee.setStata(EmployeeMessage.ON_THE_JOB);
            list.add(employee);
            System.out.println(EmployeeMessage.YES_NO);
            String s = scanner.next();
            if (s.equals(EmployeeMessage.NO)) {
                break;
            } else if (s.equals(EmployeeMessage.YES)) {
                continue;
            } else {
                System.out.println(EmployeeMessage.INPUT_MISTAKE);
            }
        }
    }

    /**
     * 删除员工信息
     * @param list
     */
    public static void delete(List<Employee> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.DELETE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean deleted = false;
        for (int i = 0; i < list.size(); i++) {
            Employee employee = list.get(i);
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                list.remove(i);
                deleted = true;
                System.out.println(EmployeeMessage.SUCCEED);
                break;
            }
        }
        if (!deleted) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }

    /**
     * 修改员工信息
     * @param list
     */
    // 修改员工职位
    public static void updateJob(List<Employee> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.UPDATE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean updated = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(EmployeeMessage.INPUT_POSITION);
                String newJob = scanner.next();
                employee.setJob(newJob);
                employee.setUpdatatime(LocalDateTime.now());
                updated = true;
                System.out.println(EmployeeMessage.SUCCEED);
                break;
            }
        }
        if (!updated) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }

    }

    // 修改员工工资
    public static void updateSalary(List<Employee> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.UPDATE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean updated = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(EmployeeMessage.INPUT_SALARY);
                double newSalary = scanner.nextDouble();
                employee.setSalary(newSalary);
                employee.setUpdatatime(LocalDateTime.now());
                updated = true;
                System.out.println(EmployeeMessage.SUCCEED);
                break;
            }
        }
        if (!updated) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }

    // 修改员工个人信息
    public static void updateInformation(List<Employee> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.UPDATE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean updated = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(EmployeeMessage.INPUT_PHONE+EmployeeMessage.INPUT_A);
                while (true){
                    String newPhone = scanner.next();
                    if(newPhone.equals(EmployeeMessage.CHOOSE_A)){
                        employee.setPhone(employee.getPhone());
                        break;
                    }else if (newPhone.length() != 11) {
                        System.out.println(EmployeeMessage.INPUT_MISTAKE);
                    }else {
                        employee.setPhone(newPhone);
                        break;
                    }
                }
                System.out.println(EmployeeMessage.INPUT_IDCARD+EmployeeMessage.INPUT_A);
                while (true){
                    String newIdcard = scanner.next();
                    if(newIdcard.equals(EmployeeMessage.CHOOSE_A)){
                        employee.setIdCard(employee.getIdCard());
                        break;
                    }else if (newIdcard.length() != 18) {
                        System.out.println(EmployeeMessage.INPUT_MISTAKE);
                    }else {
                        employee.setIdCard(newIdcard);
                        break;
                    }
                }
                System.out.println(EmployeeMessage.INPUT_ADDRESS+EmployeeMessage.INPUT_A);
                String newAddress = scanner.next();
                if(newAddress.equals(EmployeeMessage.CHOOSE_A)){
                    employee.setAddress(employee.getAddress());
                }else {
                    employee.setAddress(newAddress);
                }
                updated = true;
                System.out.println(EmployeeMessage.SUCCEED);
                break;
            }
        }
        if (!updated) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }

    /**
     * 查询员工信息
     * @param list
     */
    //查询全部
    public static void view(List<Employee> list) {
        if(list.isEmpty()){
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }else {
            System.out.println(EmployeeMessage.EMPLOYEE);
            System.out.println(EmployeeMessage.SEPARATE);
            for(Employee employee:list){
                System.out.println(employee);
                System.out.println(EmployeeMessage.SEPARATE);
            }
        }

    }
    // 查询单个员工信息
    public static void viewOne(List<Employee> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.SELCETE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean updated = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(employee);
                updated = true;
                break;
            }
        }
        if (!updated) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }

    /**
     * 员工状态管理
     */
    // 查看员工状态
    public static void selectStatus(List<Employee> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.SELCETE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean found = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(employee.toString2());
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }

    public static void viewStatus(List<Employee> list) {
        if(list.isEmpty()){
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }else {
            System.out.println(EmployeeMessage.SEPARATE);
            for(Employee employee:list){
                System.out.println(employee.toString2());
                System.out.println(EmployeeMessage.SEPARATE);
            }
        }
    }

    // 修改员工状态
    public static void updateStatus(List<Employee> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(EmployeeMessage.UPDATE_EMPLOYEE_IDORNAME);
        String input = scanner.next();
        boolean updated = false;
        for (Employee employee : list) {
            if (String.valueOf(employee.getId()).equals(input) || employee.getName().equals(input)) {
                System.out.println(EmployeeMessage.NEW_STATA);
                String newStatus = scanner.next();
                employee.setStata(newStatus);
                System.out.println(EmployeeMessage.SUCCEED);
                updated = true;
                break;
            }
        }
        if (!updated) {
            System.out.println(EmployeeMessage.NO_EMPLOYEE);
        }
    }
}